

import Banner from '@components/banner/index.vue';
import Card from './_components/Card.vue';
import Menus from './_components/menus.vue'
import SelectedShop from './_components/SelectedShop.vue';
import Tab from './_components/tab.vue'
import {postClassMeiye,postfreshFocus,postHotCardList,postPickOfThe,postActivityClassMeiye,postNearbyList} from '@/request/actions';
import {GetAddress} from '@/common/getaddress';
import {Vue, Component } from 'vue-property-decorator';
import HotCard from './_components/hotCard.vue'
import PageBottom from '@components/pageBottom/index.vue'

interface Location{
    lat:number|string;
    lng:number|string;
    province:string;
    city:string;
    district:string;
    userId?:string|number;
}

interface BaseNearListEle{
    distance:number;
    isCode:string;
    salonAddress:string;
    salonId:number;
    salonName: string;
    salonPicture: string;
}


@Component({
    components:{
        
        Banner,
        Card,
        SelectedShop,
        Menus,
        Tab,
        HotCard,
        PageBottom
    }
})
export default class extends Vue {
        banners:Array<any> = [];
        triggered:boolean = false;
        hideAd:boolean =true;
        opts:any = {};
        menuList:Array<any> = [];
        sTop:number|string = 'null';
        isFixed:boolean = false;
        tabHeight:number = 45;
        tabIndex:number = 1;
        page:number = 1;
        limit:number = 6;
        list:Array<any> = [];
        bStop:boolean = true;
        activityClassMeiye:Array<any> = [];
        cardCategory:number|string = '';
        loadingType:string|number = 'none';


        getSelectId(id:number){
            this.cardCategory = id;
            this.page = 1;
            this.bStop = true;
            
            uni.pageScrollTo({  
                scrollTop:this.sTop,  
                duration:30,
                success:()=>{
                   
                    this.HttpNearbyList();

                }  
            });  
           
           
        }    

    HttpNearbyList(arr:Array<any> = []){
     
        let page = this.page;
        let limit = this.limit;
        let list = arr;
        let opts = this.opts;
        let cardCategory = this.cardCategory || null;

        
         
        //cardCategory:
 
        uni.showLoading();
        postNearbyList({
            ...opts,
            page,
            limit,
            cardCategory
        }).then((r:any)=>{
      
            let {data:res} = r;
            if(res.status == 200){
              
                list.push(...res.data.stulist);

               
                
                this.list = list;
            

               

                if(res.data.total == 0){
                    this.loadingType = 0;
                }else if(res.data.total == this.list.length){
                    this.loadingType = 1;

                }
                
            }
            uni.hideLoading();
            this.bStop = res.data.stulist.length < limit ?  false : true;
        })
    }    
        
    HttphotCardList(arr:Array<any> = []){

        let page = this.page;
        let limit = this.limit;
        let list = arr;

 
        uni.showLoading();
        postHotCardList({
            page,
            limit
        }).then((r:any)=>{
          
            let {data:res} = r;
            if(res.status == 200){
                list.push(...res.data.stulist)
                this.list = list;
               
                if(res.data.total == 0){
                    this.loadingType = 0;
                }else if(res.data.total == this.list.length){
                    this.loadingType = 1;

                }
                
            }
            uni.hideLoading();
            this.bStop = res.data.stulist.length < limit ?  false : true;
        })

    }    

    HttpPickOfThe(arr:Array<any> = []){
        let page = this.page;
        let limit = this.limit;
        let list = arr;


        uni.showLoading();
        postPickOfThe({
            page,
            limit
        }).then((r:any)=>{
          
            let {data:res} = r;
            if(res.status == 200){
                 list.push(...res.data.stulist)
                this.list = list;
             
                if(res.data.total == 0){
                    this.loadingType = 0;
                }else if(res.data.total == this.list.length){
                    this.loadingType = 1;
                    

                }
                
            }
            uni.hideLoading();
            this.bStop = res.data.stulist.length < limit ?  false : true;
        })
    }

    

    onLoad(query:BaseQuery) {


        postClassMeiye().then((r:any)=>{

            let {data:res} = r;
         
            if(res.code == 200){
                this.menuList = res.data;
                
            }

        })

        postfreshFocus({typeId:1}).then((r:any)=>{

            let {data:res} = r;


            if(res.status == 200){
                this.banners = res.data;
            }
            
        })
        this.loadData();

        this.isLogin();

        let globalData = getApp().globalData;

        if (query.hasOwnProperty('scene')) {

            let scene = decodeURIComponent(query.scene).split('&');
            let newQuery:BaseQuery = {};

            scene.forEach((item, i) => {
                let a = item.split('='); 
                let k = a[0];
                let v = a[1];
                newQuery[k] = v;   
            })
            
            globalData.query = newQuery; 
        }else{
            globalData.query = query; 
        }


     
       
    }
   
    isLogin(){
        let globalData = getApp().globalData;

        

        if(Object.keys(globalData.userInfo).length == 0){
            uni.showModal({
                content:"请先登陆",
                success: (res:any)=> {
                        if (res.confirm) {
                        return  uni.navigateTo({
                                    url: `/pages/login/index`
                                });
                        }
                    }
                })
            }
    }
    onPullDownRefresh(){
        
        this.loadData();

     }
    switchHttp(n:number,arr:Array<any> = []){
    

         

            switch (n) {
                case 1:
                    this.HttpPickOfThe(arr);
                    break;
                case 2:
                    this.HttphotCardList(arr);
                    break;   
                case 3:
                    this.HttpNearbyList(arr);
                    break;        
                    
                default:
                    break;
            }

            

    
    }
        loadData(){

        
        
           this.page = 1;
           this.bStop = true;
           this.list = []; 
          this.tabIndex = 1;
          this.switchHttp(1,this.list);
          this.loadingType = 'null';


          postActivityClassMeiye().then((r:any)=>{
                let {data:res} = r;
                if(res.code == 200){
                   
                    this.activityClassMeiye = res.data;
                }
          })


    }


     onPageScroll(e:any){
        const query = uni.createSelectorQuery().in(this);
         query.select('#tab').boundingClientRect((data:any) => {
            
               
                this.tabHeight = data.height;
             
           
         }).exec();
        if(this.sTop == 'null'){
              
               query.select('#top').boundingClientRect((data:any) => {
                  this.sTop = data.height;
              }).exec();
        }else{
                let scrollTop = e.scrollTop;       
                this.isFixed = scrollTop >= this.sTop?true:false;
        }
         

         
      }

      onReachBottom(){

        this.page++;
          
        if(!this.bStop) return;


        this.switchHttp(this.tabIndex,this.list);
        
       
      }

    async onShow(){
         
        let getAddress = new GetAddress();
        let r_address = await getAddress.getData();
        let res_address = r_address.data || r_address;
        this.loadingType = 'null';
   
        let province = '';
        let city = '';
        let district = '';
        let lat = '';
        let lng = '';
        
        let globalData:BaseGlobalData = getApp().globalData;
       
       
       
        if(res_address.message == "query ok"){
               let {address:name,location,address_component } = res_address.result;   
               ({lat:lat,lng:lng} = location);
               ({province:province,city:city,district:district} = address_component); 


               uni.setStorageSync('startPoint',JSON.stringify({
                  name,
                  lat:location.lat,
                  lng:location.lng,
                  address_component:address_component
                }))
                
              
        }

     
       
        var opts:Location = {lat,lng,province,city,district } 
        if(Object.keys(globalData.userInfo)){
                let {userId} =globalData.userInfo;
                opts = {...opts,userId}
        }

        this.opts = opts;
       

     

      }

      getTabIdx(i:number){
          if(this.tabIndex == i)return;
  
            this.tabIndex = i;
            this.bStop = true;
            this.page = 1;
            this.cardCategory = '';

         


            this.loadingType = 'null';
            this.list = [];
            this.switchHttp(this.tabIndex);
            

            (this.isFixed && uni.pageScrollTo({  
                scrollTop:this.sTop,  
                duration:30,
            }));  
        
       
      }
      

}